
//终端命令下发结果查询类
TerminalCommand = {};

TerminalCommand.timerName = "timerForQuery";
TerminalCommand.times=60;//下发命令后，等待应答倒计时60秒
TerminalCommand.onSuccess=null;
TerminalCommand.commandButton = null;//下发命令的按钮
TerminalCommand.commandButtonText = "";
TerminalCommand.messageDiv = null;//显示后台消息的DIV 控件
TerminalCommand.body = null;//body控件
TerminalCommand.startQueryResult = function(commandId,url, onSuccess)
{ 
	if(!url)
		 url = globalConfig.webPath + "/command/getCommandResult.action"; //查询命令的执行情况
	if(!this.commandButton)
		this.commandButton = $('.sendjson');
	if(!this.messageDiv)
		this.messageDiv = $('.commandMsg');
	this.commandButtonText = this.commandButton.text();
	if(this.commandButtonText.length == 0)
		this.commandButtonText = this.commandButton.val();
	if(!this.body)
		this.body = $('body');
	this.url = url;
	var me = this;
	this.times = 60;//获取查询结果的最大次数
	this.messageDiv.html("");
	this.commandButton.attr("disabled","disabled");
	//this.commandButton.val("等待终端返回结果("+this.times+" )");
	this.setCommandBttonText("等待终端返回结果("+this.times+" )");
	  //使用jquery.timer插件，进行定时调用，最多执行次数取决this.times的次数定义
	$('body').oneTime('500ms', this.timerName,function(){  
		 me.queryResult(commandId); //查询命令的返回结果
	});
	this.onSuccess = onSuccess;
}
//停止查询和倒计时
TerminalCommand.stopQuery = function()
{
    TerminalCommand.setCommandBttonText();
	$('body').stopTime (this.timerName);
}

TerminalCommand.setCommandBttonText = function(text)
{
	if(text == null)
		text = TerminalCommand.commandButtonText;
	if(TerminalCommand.commandButton)
	{
        TerminalCommand.commandButton.val(text);
        if(TerminalCommand.commandButton[0] && TerminalCommand.commandButton[0].lastChild != null)
            TerminalCommand.commandButton[0].lastChild.textContent = text;

	}
}

//查询命令执行结果
TerminalCommand.queryResult = function(commandId)
{	
    this.times--;
	var me = this;
	this.setCommandBttonText("等待终端执行("+this.times+" )");
	var params = {commandId:commandId};
	$.getJSON(this.url, params, 
			function(result){		
				  if(result.success == true)
				 {
					me.setCommandBttonText();
					me.commandButton.attr("disabled",false); 
					me.messageDiv.html(result.message);
					if(me.onSuccess)
					{
						me.onSuccess(result);
					}
				}else {
				   me.messageDiv.html(result.message);
				   if(me.times == 1)
				   {
						 
						me.messageDiv.html("命令执行超时，无应答,请重新操作");
					    //me.commandButton.val(me.commandButtonText);

                       me.setCommandBttonText();
						me.commandButton.attr("disabled",false); 
					    $('body').stopTime ();
				   }else if(result.message == "等待发送" && me.times <= 50)
					{
						me.messageDiv.html("服务器没有下发,请重新操作");
						
					    //me.commandButton.val(me.commandButtonText);

                        me.setCommandBttonText();
						me.commandButton.attr("disabled",false); 
					    $('body').stopTime ();
					}
				   else{
					     //最多执行5次
						 $('body').oneTime('500ms', me.timerName,function(){  
								 me.queryResult(commandId); //查询命令的返回结果
						});
					}
					
				}
		  }
	  );
}














/**
 * 
 * 以下为上面的类重写
 * 
 * ps：使用TerminalCommand2
 * 需要定义
 * 1.messgeDiv，目的是不让服务器下发的信息发到所有的class相同的信息框中
 * 2. timerName,目的是不让服务器停止所有的定时器或者开启所有的定时器
 * 3.commandButton ,目的是区分按钮，不会让所有的相同class按钮显示都显示下发的字体
 * 
 */





var TerminalCommand2 = function(){

};

TerminalCommand2.prototype.timerName = "timerForQuery";
TerminalCommand2.prototype.times=60;//下发命令后，等待应答倒计时60秒
TerminalCommand2.prototype.onSuccess=null;
TerminalCommand2.prototype.commandButton = null;//下发命令的按钮
TerminalCommand2.prototype.commandButtonText = "";
TerminalCommand2.prototype.messageDiv = null;//显示后台消息的DIV 控件
TerminalCommand2.prototype.body = null;//body控件

TerminalCommand2.prototype.startQueryResult = function (commandId, url, onSuccess) {
	try {
		if (!url)
			url = globalConfig.webPath + "/command/getCommandResult.action"; //查询命令的执行情况
		if (!this.commandButton)
			this.commandButton = $('.sendjson');
		if (!this.messageDiv)
			this.messageDiv = $('.commandMsg');
		this.commandButtonText = this.commandButton.text();
		if (this.commandButtonText.length == 0)
			this.commandButtonText = this.commandButton.val();
		if (!this.body)
			this.body = $('body');

		console.log(this.messageDiv);
		console.log(this.commandButton);
		console.log(this.body);
		this.url = url;
		var me = this;
		this.times = 60;//获取查询结果的最大次数
		this.messageDiv.html("");
		this.commandButton.attr("disabled", "disabled");

		//this.commandButton.val("等待终端返回结果("+this.times+" )");
		this.setCommandBttonText("等待终端返回结果(" + this.times + " )");
		//使用jquery.timer插件，进行定时调用，最多执行次数取决this.times的次数定义
		me.body.oneTime('500ms', me.timerName, function () {
			me.queryResult(commandId); //查询命令的返回结果
		});
		// this.newtimer = window.setInterval(me.queryResult(commandId),500);
		this.onSuccess = onSuccess;
	} catch (error) {
		console.log(error);
	}

};



//停止查询和倒计时
TerminalCommand2.prototype.stopQuery = function()
{
    this.setCommandBttonText();
	this.body.stopTime (this.timerName);
};

TerminalCommand2.prototype.setCommandBttonText = function(text)
{
	if(text == null)
		text = this.commandButtonText;
	if(this.commandButton)
	{
        this.commandButton.val(text);
        if(this.commandButton[0] && this.commandButton[0].lastChild != null)
        this.commandButton[0].lastChild.textContent = text;
	}
};

//查询命令执行结果
TerminalCommand2.prototype.queryResult = function (commandId) {
	try {
		
		this.times--;
		var me = this;
		me.setCommandBttonText("等待终端执行(" + me.times + " )");
		var params = { commandId: commandId };
		console.log(this.times);
		$.getJSON(me.url, params,
			function (result) {
				console.log("success="+result.success+",messge="+result.message+",buttonText="+me.commandButtonText);
				console.log(me.commandButton);
				if (result.success == true) {
					me.setCommandBttonText();
					me.commandButton.attr("disabled", false);
					me.messageDiv.html(result.message);
					if (me.onSuccess) {
						me.onSuccess(result);
					}
				} else {
					me.messageDiv.html(result.message);
					if (me.times == 1) {

						me.messageDiv.html("命令执行超时，无应答,请重新操作");
						//me.commandButton.val(me.commandButtonText);

						me.setCommandBttonText();
						me.commandButton.attr("disabled", false);
						me.body.stopTime(me.timerName);
					} else if (result.message == "等待发送" && me.times <= 50) {
						me.messageDiv.html("服务器没有下发,请重新操作");

						//me.commandButton.val(me.commandButtonText);

						me.setCommandBttonText();
						me.commandButton.attr("disabled", false);
						me.body.stopTime(me.timerName);
					}
					else {
						//最多执行5次
						me.body.oneTime('500ms', me.timerName, function () {
							me.queryResult(commandId); //查询命令的返回结果
						});
					}

				}
			}
		);

	} catch (error) {
		console.log(error);
	}

};
